const app = getApp();
const companyName = app.globalData.companyName;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: '',
    name: '',
    phone: '',
    code: '',
    multiArray: [],
    multiIndex: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      multiIndex: [0, 0]
    });
    this.pickerTap();
  },

  pickerTap: function() {
    var date = new Date();
    var monthDay = ['今天', '明天'];
    var times = [];
    for (var i = 2; i <= 7; i++) {
      var date1 = new Date(date);
      date1.setDate(date.getDate() + i);
      var md = (date1.getMonth() + 1) + "月" + date1.getDate() + '日';
      monthDay.push(md);
    }
    const dayIndex = this.data.multiIndex[0];
    if (dayIndex === 0) {
      const hour = date.getHours() + 1;
      for (var i = hour; i <= 22; i++) {
        if (i < 10) {
          times.push('0' + i + ':00');
        } else {
          times.push(i + ':00');
        }
      }
    } else {
      for (var i = 8; i <= 22; i++) {
        if (i < 10) {
          times.push('0' + i + ':00');
        } else {
          times.push(i + ':00');
        }
      }
    }
    const multiArray = [];
    multiArray[0] = monthDay;
    multiArray[1] = times;
    this.setData({
      multiArray,
      time: '今天 ' + times[0]
    });
  },
  bindMultiPickerChange: function(e) {
    const multiArray = this.data.multiArray;
    const dayArray = multiArray[0];
    const timeArray = multiArray[1];
    const dayIndex = e.detail.value[0];
    const timeIndex = e.detail.value[1];
    const dayText = dayArray[dayIndex];
    const timeText = timeArray[timeIndex];
    this.setData({
      multiIndex: e.detail.value,
      time: dayText + ' ' + timeText
    })
  },
  bindMultiPickerColumnChange: function(e) {
    var times = [];
    const multiArray = this.data.multiArray;
    const multiIndex = this.data.multiIndex;
    multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        switch (multiIndex[0]) {
          case 0:
            var date = new Date();
            const hour = date.getHours() + 1;
            for (var i = hour; i <= 22; i++) {
              if (i < 10) {
                times.push('0' + i + ':00');
              } else {
                times.push(i + ':00');
              }
            }
            multiArray[1] = times;
            break;
          case 1:
            for (var i = 8; i <= 22; i++) {
              if (i < 10) {
                times.push('0' + i + ':00');
              } else {
                times.push(i + ':00');
              }
            }
            multiArray[1] = times;
            break;
        }
        multiIndex[1] = 0;
        multiIndex[2] = 0;
        break;
      case 1:
        switch (multiIndex[0]) {
          case 0:
            var date = new Date();
            const hour = date.getHours() + 1;
            for (var i = hour; i <= 22; i++) {
              if (i < 10) {
                times.push('0' + i + ':00');
              } else {
                times.push(i + ':00');
              }
            }
            multiArray[1] = times;
            break;
          case 1:
            for (var i = 8; i <= 22; i++) {
              if (i < 10) {
                times.push('0' + i + ':00');
              } else {
                times.push(i + ':00');
              }
            }
            multiArray[1] = times;
            break;
        }
        multiIndex[2] = 0;
        break;
    }
    this.setData({
      multiArray,
      multiIndex
    });
  },
  getCode: function() {
    const phone = this.data.phone;
    if (phone.length === 0) {
      wx.showToast({
        title: '请输入手机号码',
        icon: 'none'
      });
      return;
    }
    const url = app.globalData.url;
    wx.request({
      url: url + '/management/traineeCode/' + phone,
      success: function(response) {
        if (response.data.success) {
          wx.showToast({
            title: '验证码已经发送,请注意查看',
            icon: 'none'
          });
        } else {
          wx.showToast({
            title: '提取验证码失败:' + response.data.message,
            icon: 'none'
          });
        }
      }
    })
  },
  inputPhone: function(e) {
    this.setData({
      phone: e.detail.value
    });
  },
  inputCode: function(e) {
    this.setData({
      code: e.detail.value
    });
  },
  inputName: function(e) {
    this.setData({
      name: e.detail.value
    });
  },
  toBook: function() {
    const name = this.data.name;
    let time = this.data.time;
    const phone = this.data.phone;
    const code = this.data.code;
    if (name.length === 0) {
      wx.showToast({
        title: '请输入名字',
        icon: 'none'
      });
      return;
    }
    if (phone.length === 0) {
      wx.showToast({
        title: '请输入联系方式',
        icon: 'none'
      });
      return;
    }
    if (code.length === 0) {
      wx.showToast({
        title: '请输入验证码',
        icon: 'none'
      });
      return;
    }
    //转换
    const date = new Date();
    if(time.indexOf('今天')>=0){
      var md = (date.getMonth() + 1) + "月" + date.getDate() + '日';
      time = time.replace('今天', md);
    } else if (time.indexOf('明天') >= 0){
      date.setDate(date.getDate() + 1);
      var md = (date.getMonth() + 1) + "月" + date.getDate() + '日';
      time = time.replace('明天', md);
    }
    const url = app.globalData.url;
    wx.request({
      url: url + '/train/appointmentToCompany',
      data: {
        companyName,
        name,
        appointmentTime: time,
        phone,
        code
      },
      method: 'POST',
      success: function(response) {
        if (response.data.success) {
          wx.navigateBack({
            delta: 1
          });
          wx.showToast({
            title: '预约到店成功',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: '预约到店失败',
            icon: 'none'
          })
        }
      }
    })
  }
})